package von.seiji.cn.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import von.seiji.cn.IpUtil;
import von.seiji.cn.SelfUtil;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: Amasawa Seiji
 * @DateTime: 2020/12/19 5:25
 * @Description: 暂无描述
 */

@RestController
@RequestMapping("/util")
public class UtilController {

    @RequestMapping
    public String methods_details() {
        Method[] methods = UtilController.class.getDeclaredMethods();
        StringBuffer buffer = new StringBuffer("<html  style=\"font-family:'楷体';background-color:black\">");
        buffer.append("<head><title>与你不期而遇~</title></head>");
        buffer.append("<body ondragstart=\"return false\" oncontextmenu=\"return false\" onselectstart=\"return false\">");
        for (Method method : methods) {
            /*οncοntextmenu=”window.event.returnValue=false” （防止右键）
οndragstart=”window.event.returnValue=false” （防止拖动）*/

            if (Lists.newArrayList("methods_details", "lambda$methods_details$0").contains(method.getName())) continue;

            buffer.append("<h2 style='color:red;'><span>方法名：</span>").append(method.getName()).append("</h2>");
            buffer.append("<p  style='color:#fff;'><span>请求URL：</span>" + JSON.toJSONString(method.getAnnotation(RequestMapping.class).value()) + "</p>");
            List<HashMap<String, Object>> collect = Arrays.stream(method.getParameters()).map(item -> {
                HashMap<String, Object> map = Maps.newHashMap();
                map.put("name", item.getName());
                map.put("type", item.getType());
                return map;
            }).collect(Collectors.toList());
            buffer.append("<p  style='color:#eee;'><span>方法参数：</span>" + JSON.toJSONString(collect) + "</p>");
            buffer.append("<p style='color:#ddd;'><span>方法返回类型：</span>" + JSON.toJSONString(method.getReturnType()) + "</p>");

        }
        buffer.append("<script>document.onkeydown = function (event){event.preventDefault()}</script>");
        buffer.append("< Script Language=javascript> function Click(){ if(event.button!=1){alert ('hello~~~'); }} document.onmousedown=Click; < /Script>");
        buffer.append("</body>");
        buffer.append("<footer style='color:#0f0;'>&copy;Seiji926<天沢聖司></footer>");
        buffer.append("</html>");
        return buffer.toString();
    }

    @RequestMapping("/ip_search")
    public Map ip_search() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String a = IpUtil.getIpAddr(request);
        HashMap b = IpUtil.getAddresses(a);
        return b;
    }

    @RequestMapping("/ik_analyze")
    public List ik_analyze(@RequestParam("arg0") String text) {
        List<String> b = SelfUtil.analyzer(text, true);
        return b;
    }

}
